package com.ferry.common.util;
import	java.io.PrintStream;
import	java.io.ByteArrayOutputStream;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

/**
 * Created by daiyong
 */
public class StackTraceUtil {

	private static final String NULL_STRING = "null";

	//私有构造函数 防止重复构建
	private StackTraceUtil() {}

	public static String stackTrace(Throwable throwable) {
		if (throwable == null) {
			return NULL_STRING;
		} else {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			PrintStream out = new PrintStream(baos);
			throwable.printStackTrace(out);
			out.flush();

			try {
				return new String(baos.toByteArray());
			} finally {
				try {
					baos.close();
				} catch (IOException ignored) {}
			}
		}
	}

}
